﻿using System;
using System.Windows.Threading;
using System.Threading.Tasks;

namespace HobSoft.SoccerBet2014.Client.Common
{
    using Domain.Schedule;
    public class ScheduleManagerTimer : ITimer
    {
        private readonly DispatcherTimer timer;
        public ScheduleManagerTimer()
        {
            timer = new DispatcherTimer();
            timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            RaiseTick();
        }

        private void RaiseTick()
        {
            if (Tick != null)
            {
                Task.Factory.StartNew(() =>
                {
                    Tick(this, EventArgs.Empty);
                });
            }
        }

        public double Interval
        {
            get
            {
                return timer.Interval.TotalMilliseconds;
            }
            set
            {
                timer.Interval = TimeSpan.FromMilliseconds(value);
            }
        }

        public object Tag
        {
            get
            {
                return timer.Tag;
            }
            set
            {
                timer.Tag = value;
            }
        }

        public void Start()
        {
            timer.Start();
        }

        public void Stop()
        {
            timer.Stop();
        }

        public event EventHandler Tick;
    }
}
